#!/usr/local/bin/python2.4
# -*- coding: UTF-8 -*-
# visualisering hotet
# kristoffer josefsson 2006

from vj import *
from texturegl import *
from LSystemGL import *
from melt import *
from shader import *

import flickr
import urllib

from random import *
import string

class Melt2CubeRecreator(RecreatingCubeRecreator):

	def newside(self):
		i = randint(10,15)	# disabled
		if i in range(0,5):
			return TTFBillboard("hotet","vectroic.ttf", 30)
		if i in range(5,6):
			return TTFBillboard("visuals: krute.com", "register.ttf",9)
		else:
			return CABillboard()

class BridgeCubeRecreator(RecreatingCubeRecreator):

	def __init__(self,nrsides):
		RecreatingCubeRecreatorThreaded.__init__(self,nrsides)

	def newside(self):
		bill = BridgeBillboard("bartender.png")		
		return bill

class FlickrCubeRecreator(RecreatingCubeRecreator):

	def __init__(self,nrsides):
		RecreatingCubeRecreatorThreaded.__init__(self,nrsides)
		print("Searching for photos on flickr.com (slow)")
		self.search = flickr.photos_search(tags='fish', per_page=20)

	def newside(self):
	
		print("Choosing photo")
		url = self.search[randint(0,len(self.search)-1)].getURL(size='Medium', urlType='source')
		print("Getting url")
		file, mime = urllib.urlretrieve(url)
		print("Getting image")
		photo = Image.open(file)
		print("Building image")
		bill = BridgeBillboard(file)		
	
		bill.image = self.billt.image
		bill.make()   		
	
		return bill

class Bridge(Scene):
	def	__init__(self):
		Scene.__init__(self, 720, 576)
		
		self.title = "hotet@ideal ! nef"
		
		self.rtri =	0.5
		self.triRate = 3.0
		self.raxis = [0,1,0]
		self.fov = 30
		self.dizzy = var(False)
		self.ls = var(True)
		self.logo = var(False)
		self.melt = var(True)
		self.text = var(False)


	def regenerate(self):
		
		Scene.regenerate(self)
		
		i = randint(0,100)
			
		if i in range(0,30):
			self.dizzy.toggle()
			
		if i in range(2,10):
			self.melt.toggle()
			
		if i in range(4,40):
			self.text.toggle()
			
		if i in range(30,40):
			self.logo.toggle()
		
		if i in range(40,60):
			self.ls.toggle()
			
		if i in range(0,100):
			self.lsgl = LSystemGL(uniform(0.6,2),uniform(-0.4,0.5))	

		if i in range(10,40):
			self.triRate = randint(-5,5)
			if self.triRate == 0:
				self.dizzy = True		
		if i in range(60,150):
			self.fov = randint(20,170)
		if i in range(0,100):
			self.interval = randint(10,100)

		if i in range(30,65):
			self.bill.recreate()

		if i in range(0,30):
			self.raxis = [1,0,0]
		if i in range(31,62):
			self.raxis = [0,1,0]
		if i in range(63,100):
			self.raxis = [0,0,1]		

	def	init(self):
	
		Scene.init(self)
		
		self.back = ImageBillboard(["../images/hotet_logo.jpg","../images/hotet_logo.jpg"][randint(0,1)])
		self.krute = TTFBillboard("hotet", "../fonts/vectroic.ttf",20 + randint(-1,1),1024)
		self.bill = RecreatingCube(Melt2CubeRecreator,1)
		
		self.lsgl = LSystemGL(uniform(0.6,2),uniform(-0.4,0.5))
		
		self.shader = MyMaterial("../shaders/sphere.glsl")


	def	display(self):
		Scene.display(self)
		
		for side in self.bill.sides:
			side.offset = self.rtri/100
		
		glTranslatef(-0.5,-0.5,-1)
		glColor4f(0.3,0.3,0.3,0.4)
		glDisable(GL_DEPTH_TEST)
		glEnable(GL_BLEND)
	#	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA)
		glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA)
	#	glLineWidth(2.0)
	#	self.back.draw()
		glPushMatrix()
		glLoadIdentity()
		glRotatef(self.rtri,self.raxis[0],self.raxis[1],self.raxis[2])
		glTranslatef(-0.5,-0.5,0.5)
		
		if self.dizzy.true:
			glScalef(1+sin(self.t1*2)/8,1+sin(self.t1*4)/8,1+sin(self.t1*8)/8);
				
		self.bill.draw()
		glPopMatrix()
		
		glPushMatrix()
		glTranslatef(0.0,0,0.5+random()*sin(self.rtri)/3)
		if self.text.true:
			self.krute.draw()
		glPopMatrix()
		
		
		glPushMatrix()
		glTranslatef(0.0,0,-0.2)
		if self.logo.true:
			self.back.draw()
		glPopMatrix()
		
		glRotatef(self.rtri*5, 1,1,1)
		
		self.shader.activate()

		self.loc1 = glGetUniformLocationARB(self.shader.m_program,"n")
		glUniform1fARB(self.loc1,3.0)

		self.loc1 = glGetUniformLocationARB(self.shader.m_program,"LightPosition")
		self.loc2 = glGetUniformLocationARB(self.shader.m_program,"SurfaceColor")		glUniform3fARB(self.loc1,1.0,1.0,1.0)		glUniform3fARB(self.loc2,1.0,0.5+sin(self.rtri/10.0)/2.0,0.5+sin(self.rtri/10.0)/2.0)	

		self.loc1 = glGetUniformLocationARB(self.shader.m_program,"Radius")
		self.loc2 = glGetUniformLocationARB(self.shader.m_program,"Blend")		glUniform1fARB(self.loc1,2.0*sin(0.1*self.rtri+1.0)/2.0)		glUniform1fARB(self.loc2,2.0*cos(1.53+self.rtri/5.0)/20.0)	
		
		if self.ls.true:
			self.lsgl.draw()
		
		self.shader.deactivate()
		
		self.lsgl.update(sin(self.rtri/20.0))

		glutSwapBuffers()


	def	keyboard(self, key,	x, y):
		Scene.keyboard(self, key, x,y)
		
		if key == 'r':
			self.init()
			self.bill.recreate()
		
		if key == 'o':
			for side in self.bill.sides:
				side.offset -= 0.01
		
		if key == 'O':
			for side in self.bill.sides:
				side.offset += 0.01

		if key == 'h':
			for side in self.bill.sides:
				side.height -= 0.1
		
		if key == 'H':
			for side in self.bill.sides:
				side.height += 0.1
				
		if key == '1':
			self.bill.recreate()

		if key == '2':
			self.regenerate()
			
		if key == 'x':
			self.raxis = [1,0,0]
		if key == 'y':
			self.raxis = [0,1,0]
		if key == 'z':
			self.raxis = [0,0,1]
		
		if key == '+':
			self.triRate += 1
		if key == '-':
			self.triRate -= 1	

		if key == 's':
			self.dizzy.toggle()	
			
		if key == '9':
			self.logo.toggle()
			
		if key == '8':
			self.text.toggle()
			
		if key == '7':
			self.ls.toggle()
			
		if key == '6':
			self.melt.toggle()	
				
if __name__=="__main__":
	
#	try:
#        	import psyco
#		psyco.full()
#   	except:
# 	     	print 'Psyco not found, ignoring it'


	s=Bridge()
	launchConsole()
	s.main(sys.argv)
	
